/**
*我的js框架tvc:标签值控制
*@version v1.0
*@author : lisheng
*@date 2013-10-28
*
*注意事项：
*  1、element对象必须是原生js对象，通过jquery获取的对象需要进行转换:element = $(selector)[0]
*  2、本插件默认规则：A.元素的值为""或者select的值为-1或"",表示为空。
*  3、参数except={id:,name:,type:}，id:表示例外的元素的id;name：表示例外的元素的name;type:表示例外的元素的type。
*     其中""表示不参与例外比较。
*属性说明：
* debug：是否开启调试模式（默认是不开启）
*  
*方法说明：
**
 * 元素的值是否是空值
 * @param element:元素数组
 *
1、isEmpty(element)
**
 * 改变元素的默认值
 * @param element:元素
2、changeDefaultValue(element)
** 
 *重置元素的值
 * @param element:元素
 *
3、resetValue (element)
**
 * 元素的值是否改变
 * @param element:元素
 *
4、isChanged(element)
**
 * 多个元素中是否都是空值
 * @param elements:元素数组
 *
5、isEmptys(elements[,except])
**
 * 多个元素中：是否有空值元素
 * @param elements:元素数组
 *
6、hasEmpty(elements[,except])
**
 * 改变多个元素的默认值
 * @param elements:元素数组
 *
7、changeDefaultValues(elements[,except])
** 
 *重置多个元素的值
 * @param elements:元素数组
 *
8、resetValues (elements[,except])
**
 * 多个元素的值是否都改变
 * @param elements:元素数组
 *
9、isChangeds(elements[,except])
**
 * 多个元素的值是存在改变
 * @param elements:元素数组
 *
10、hasChanged (elements[,except])
*
*disables (elements,except)
*readOnlys (elements,except)
*enableEdits (elements,except)
*disableEdits (elements,except)
*disable (element,disabled)
*readOnly (element,flag) 
*/

(function( window, undefined ) {
  window.tvc = {};
  var tvc = {
    /**
     * 输出日志
     */
    log: function (obj) {
	    if(tvc.debug){
			try {
				console.log(obj);
			}
			catch (ex) {
			}
	    }
	} 
   ,
logElement: function (method,element) {
	    if(tvc.debug){
			try {
				var _html = "";
				_html += "method--"+method+"--";
				_html += "id:"+element.id+",";
				_html += "name:"+element.name+",";
				_html += "type:"+element.type+",";
				_html += "value:"+element.value+",";
				_html += "defaultValue:"+element.defaultValue+",";
				_html += "checked:"+element.checked+",";
				_html += "defaultChecked:"+element.defaultChecked+",";
				_html += "selected:"+element.selected+",";
				_html += "defaultSelected:"+element.defaultSelected+"";
			    
				console.log(_html);
			}
			catch (ex) {
			}
	    }
} 
,
disable : function(element,disabled){ 
	   var m_disabled = true;
	   if(disabled!=undefined && disabled!="undefined"){
		   m_disabled = disabled;
	   }
		tvc.logElement("disable",element);
	    if(element && element.type ){
		      switch(element.type){
		         case "text":
	             case "submit": 
	             case "reset": 
	             case "image":
	             //case "hidden":
	             case "textarea":
	             case "password":
	             case "file":
	             case "button": 
            	 	 element.disabled = m_disabled;
                     	break;
	             case "radio":
	             case "checkbox":  
	            	 element.disabled = m_disabled;
	                    break;
	             case "select-one":
	            	 element.disabled = m_disabled;
	            	 	break;
	             case "select-multiple":
	            	 element.disabled = m_disabled;
	                 	break;
		         default:break; 
		       } 
	    } 
 }   
  ,
readOnly : function(element,flag){ 
	   m_flag = true;
	   if(flag!=undefined && flag!="undefined"){
		   m_flag = flag;
	   }
		var flag = true;
		tvc.logElement("readOnly",element);
	    if(element && element.type ){
		      switch(element.type){
		         case "text":
	             case "submit": 
	             case "reset": 
	             case "image":
	             //case "hidden":
	             case "textarea":
	             case "password":
	             case "file":
	             case "button": 
	            	 element.readOnly = m_flag;
	            	 	break;
	             case "radio":
	             case "checkbox":  
	            	 element.readOnly = m_flag;
	                    break;
	             case "select-one":
	            	 element.readOnly = m_flag;
	            	 	break;
	             case "select-multiple":
	            	 element.readOnly = m_flag;
	                 	break;
		         default:break; 
		       } 
	    } 
 }   
 ,
/**
* 将元素的值清空
*/
empty : function(element){ 
	tvc.logElement("empty",element);
    if(element && element.type ){
	      switch(element.type){
	         case "text":
             case "submit": 
             case "reset": 
             case "image":
             //case "hidden":
             case "textarea":
             case "password":
             case "file":
             case "button": 
            	 	element.value = "";
                    break;
             case "radio":
             case "checkbox":  
            	    element.checked = false; 
                    break;
             case "select-one":
            	 var length = element.options.length;
            	 for (var j = 0; j < length; j++) {
            		 element.options[j].selected = false;
                 } 
            	 break;
             case "select-multiple":
            	 var length = element.options.length;
            	 for (var j = 0; j < length; j++) {
    	  			element.options[j].selected = false;
            	 }
                 break;
	         default:break; 
	       } 
    } 
  }  
 ,
/**
 * 元素的值是否为空
 * @param element:元素
 */
isEmpty : function(element){ 
	var flag = true;
	tvc.logElement("isEmpty",element);
    if(element && element.type ){
	      switch(element.type){
	         case "text":
             case "submit": 
             case "reset": 
             case "image":
            // case "hidden":
             case "textarea":
             case "password":
             case "file":
             case "button": 
            	    var value = element.value;
            	    value = value + "";
            	    value = value.replace(" ","");
            	    if(value==null||value==""
            	 	   ||value.length==0){
            	 		flag = true;
            	 	}else{
            	 		flag = false;
            	 	}
                    break;
             case "radio":
             case "checkbox":  
            	 	 if(element.checked){
         				flag = false; 
         			 }
                    break;
             case "select-one":
            	 if(element.value && element.value!=tvc.emptySelectValue&&($.trim(element.value)!=tvc.emptyValue)){
 					flag = false;
 				 }/*
            	 for (var j = 0; j < element.options.length; j++) {
                     if (element.options[j].selected) { 
                    	 if(element.options[j].value!=-1 && (element.options[j].value)!=""){
	                    	  flag = false; 
		                   	  break;
                    	 }
                     } 
                 }*/
            	 break;
             case "select-multiple":
            	 var length = element.options.length;
            	 for (var j = 0; j < length; j++) {
                      if (element.options[j].selected) {
                    	  if(element.options[j].value!=tvc.emptySelectValue && (element.options[j].value)!=tvc.emptyValue){
		                    	  flag = false; 
			                   	  break;
	                    	  }
	                      } 
	                  }
	                  break;
	             default:break; 
	       } 
    } 
    return flag;  
  }  
 ,
 /**
  * 修改元素的默认值
  * @param element:元素
  */
 changeDefaultValue : function(element){ 
	   tvc.logElement("changeDefaultValue",element);
	    if(element && element.type){
		      switch(element.type){
		             case "text":
		             case "submit":  
		             case "reset":  
		             case "image": 
		             //case "hidden":
		             case "textarea":
		             case "password":
		             case "file":
		             case "button":
		            	    element.defaultValue = element.value;
		                    break;
		             case "radio":
		             case "checkbox":
		            	 	element.defaultChecked = element.checked;
		                    break;
		             case "select-one":
		            	 var length = element.options.length;
		            	 for (var j = 0; j < length; j++) {
		                     if (element.options[j].selected) { 
			                   	  element.options[j].defaultSelected = true;  
		                     }else{
		                   	  	  element.options[j].defaultSelected = false;
		                     }
		                 }
		            	 break;
		             case "select-multiple":
		            	 var length = element.options.length;
		            	 for (var j = 0; j < length; j++) {
		                      if (element.options[j].selected) {
		                    	  element.options[j].defaultSelected = true;
		                      }else{
		                    	  element.options[j].defaultSelected = false;
		                      }
		                  }
		                  break; 
		             default:break; 
		       } 
	    } 
	}
 ,
 /**
  * 重置元素的值
  * @param element:元素
  */
 resetValue : function(element){ 
	tvc.logElement("resetValue",element);
	    if(element && element.type){
		      switch(element.type){
		             case "text":
		             case "submit":  
		             case "reset":  
		             case "image": 
//		             case "hidden":
		             case "textarea":
		             case "password":
		             case "file":
		             case "button":
		            	    element.value = element.defaultValue;
		                    break;
		             case "radio":
		             case "checkbox":
		            	 	element.checked = element.defaultChecked;
		                    break;
		             case "select-one":
		            	 var length = element.options.length;
		            	 for (var j = 0; j < length; j++) {
		                     if (element.options[j].defaultSelected) { 
			                   	  element.options[j].selected = true;  
		                     }else{
		                   	  	  element.options[j].selected = false;
		                     }
		                 }
		            	 break;
		             case "select-multiple":
		            	 var length = element.options.length;
		            	 for (var j = 0; j < length; j++) {
		                      if (element.options[j].defaultSelected) {
		                    	  element.options[j].selected = true;
		                      }else{
		                    	  element.options[j].selected = false;
		                      }
		                  }
		                  break; 
		             default:break; 
		       } 
	    } 
	}
,
/**
 * 元素的值是否改变
 * @param element:元素
 */
 isChanged : function(element){
	tvc.logElement("isChanged",element);
    var isChanged = false;
    if(element && element.type){
	      switch(element.type){
	             case "text":
	             case "submit":  
	             case "reset":  
	             case "image": 
	             //case "hidden":
	             case "textarea":
	             case "password":
	             case "file":
	             case "button":
	                    if(element.value != element.defaultValue){
	                           isChanged= true;
	                    }
	                    break;
	             case "radio":
	             case "checkbox":
	                    if(element.checked != element.defaultChecked){
	                           isChanged= true;
	                    }
	                    break;
	             case "select-one": 
	            	         var isExistDefault = false;
	            	         var length = element.options.length;
	                    	 for (var j = 0; j < length; j++) {
			            		 if(element.options[j].defaultSelected){
			            			 isExistDefault = true; 
			            			 break;
			            		 }
			                 } 
			            	 for (var j = 0; j <length; j++) { 
			                    if(element.options[j].selected != element.options[j].defaultSelected){
			                    	if(isExistDefault){
			                    		  if(element.value != element.options[j].value){
				                    	   	  isChanged= true;
				                    	   	  break;
			                    		  }
			                    	}else{
			                    		if(j!=0){
			                    			isChanged= true;
			                    			 break;
			                    		}
			                    	}
			                     } 
			                 } 
		            	 break;
	             case "select-multiple":
	            	 var length = element.options.length;
	            	 for (var j = 0; j < length; j++) {
	                           if(element.options[j].selected != element.options[j].defaultSelected){
	                                  isChanged= true;
	                                  break;
	                           }
	                    }
	                    break;
	             default:break; 
	       } 
    } 
    return isChanged;  
}
,
emptys : function(elements,except){ 
    tvc.log("method---emptys---"+"elements's length："+elements.length);
    if(elements){
    	var length = elements.length;
    	var element = null;
    	for(var i =0;i<length;++i){
    		element = elements[i];
    		if(!tvc.inExcept(element,except)){
	    		tvc.empty(element);
    		}
    	}
    }
}
,

/**
 * 多个元素中是否都是空值
 * @param elements:元素数组
 */
isEmptys : function(elements,except){ 
    var flag = undefined;
    tvc.log("method---isEmptys---"+"elements's length："+elements.length);
    if(elements){
    	var length = elements.length;
    	var element = null;
    	for(var i =0;i<length;++i){
    		element = elements[i];
    		if(!tvc.inExcept(element,except)){
	    		if(!tvc.isEmpty(element)){
	    			flag = false;
	    			break;
	    		}
    		}
    	}
    }
	return flag;
}
,

/**
 * 多个元素中：是否有空值元素
 * @param elements:元素数组
 */
hasEmpty : function(elements,except){ 
    var flag = undefined;
    tvc.log("method---hasEmpty---"+"elements's length："+elements.length);
    if(elements){
    	var length = elements.length;
    	var element = null;
    	for(var i =0;i<length;++i){
    		element = elements[i];
    		if(!tvc.inExcept(element,except)){
	    		if(tvc.isEmpty(element)){
	    			flag = true;
	    			break;
	    		}
    		}
    	}
    }
	return flag;
}
,
/**
 * 改变多个元素的默认值
 * @param elements:元素数组
 */
changeDefaultValues : function(elements,except){ 
	 tvc.log("method---changeDefaultValues---"+"elements's length："+elements.length);
    if(elements){
    	var length = elements.length;
    	var element = null;
    	for(var i =0;i<length;++i){
    		element = elements[i];
    		if(!tvc.inExcept(element,except)){
    			tvc.changeDefaultValue(element);
    		}
    	}
    }
},
/**
 * 重置多个元素的值
 * @param elements:元素数组
 */
resetValues : function(elements,except){ 
	 tvc.log("method---resetValues---"+"elements's length："+elements.length);
	if(elements){
    	var length = elements.length;
    	var element = null;
    	for(var i =0;i<length;++i){
    		element = elements[i];
    		if(!tvc.inExcept(element,except)){
    			tvc.resetValue(element);
    		}
    	}
    }
},

/**
 * 多个元素的值是否都改变
 * @param elements:元素数组
 */
isChangeds : function(elements,except){
	 tvc.log("method---isChangeds---"+"elements's length："+elements.length);
	    var flag = undefined;
	    if(elements){
	    	var length = elements.length;
	    	var element = null;
	    	for(var i =0;i<length;++i){
	    		element = elements[i];
	    		if(!tvc.inExcept(element,except)){
		    		if(!tvc.isChanged(element)){
		    			flag = false;
		    			break;
		    		}
	    		}
	    	}
	    }
		return flag;
},
/**
 * 多个元素的值是否存在改变的
 * @param elements:元素数组
 */
hasChanged : function(elements,except){
	 tvc.log("method---hasChanged---"+"elements's length："+elements.length);
    var flag = undefined;
    if(elements){
    	var length = elements.length;
    	var element = null;
    	for(var i =0;i<length;++i){
    		element = elements[i];
    		if(!tvc.inExcept(element,except)){
	    		if(tvc.isChanged(element)){
	    			flag = true;
	    			break;
	    		}
    		}
    	}
    }
	return flag;
},
disables :function(elements,except){
	 tvc.log("method---disables---"+"elements's length："+elements.length);
	 if(elements){
	    	var length = elements.length;
	    	var element = null;
	    	for(var i =0;i<length;++i){
	    		element = elements[i];
	    		if(!tvc.inExcept(element,except)){
		    		tvc.disable(element);
	    		}
	    	}
	    }
}

,
readOnlys :function(elements,except){
	 tvc.log("method---readOnlys---"+"elements's length："+elements.length);
	 if(elements){
	    	var length = elements.length;
	    	var element = null;
	    	for(var i =0;i<length;++i){
	    		element = elements[i];
	    		if(!tvc.inExcept(element,except)){
		    		tvc.readOnly(element);
	    		}
	    	}
	    }
}

,
enableEdits :function(elements,except){
	 tvc.log("method---disables---"+"elements's length："+elements.length);
	 if(elements){
	    	var length = elements.length;
	    	var element = null;
	    	for(var i =0;i<length;++i){
	    		element = elements[i];
	    		if(!tvc.inExcept(element,except)){
		    		tvc.disable(element,false);
		    		tvc.readOnly(element,false);
	    		}
	    	}
	    }
}
,
disableEdits :function(elements,except){
	 tvc.log("method---disables---"+"elements's length："+elements.length);
	 if(elements){
	    	var length = elements.length;
	    	var element = null;
	    	for(var i =0;i<length;++i){
	    		element = elements[i];
	    		if(!tvc.inExcept(element,except)){
		    		tvc.disable(element);
		    		tvc.readOnly(element);
	    		}
	    	}
	    }
}
,
/**
 * 该元素是否为例外的元素
 */
inExcept : function(element,except){
	var isExist = false;
	var m_exceptType = null;
	var m_exceptName = null;
	var m_exceptId = null;
	if(except && except!=undefined){
		m_exceptType = except.type;
		m_exceptName = except.name ;
		m_exceptId = except.id ;
	}
	if(!m_exceptType){
		m_exceptType = tvc.exceptType;
	}
	if(!m_exceptName){
		m_exceptName = tvc.exceptName;
	}
	if(!m_exceptId){
		m_exceptId = tvc.exceptId;
	}
	var length = m_exceptType.length;
	for(var i = 0;i<length; ++i){
		var type = m_exceptType[i];
		if(type!=""&&type==element.type){
			isExist = true;
			break;
		}
	}
	if(isExist){
		tvc.log("method---inExcept:exceptType---"+"[element.name："+element.name+",element.type："+element.type+"] in ["+m_exceptType+"]");
	}else{
		tvc.log("method---inExcept:exceptType---"+"[element.name："+element.name+",element.type："+element.type+"] not in ["+m_exceptType+"]");
		var length = m_exceptName.length;
		for(var i = 0;i<length; ++i){
			var name = m_exceptName[i];
			if(name!=""&&name==element.name){
				isExist = true;
				break;
			}
		}
		if(isExist){
			tvc.log("method---inExcept:exceptName---"+"[element.name："+element.name+",element.type："+element.type+"] in ["+m_exceptName+"]");
		}else{
			tvc.log("method---inExcept:exceptName---"+"[element.name："+element.name+",element.type："+element.type+"] not in ["+m_exceptName+"]");
			var length = m_exceptId.length;
			for(var i = 0;i<length; ++i){
				var id = m_exceptId[i];
				if(id!=""&&id==element.id){
					isExist = true;
					break;
				}
			}
			if(isExist){
				tvc.log("method---inExcept:exceptId---"+"[element.name："+element.name+",element.type："+element.type+"] in ["+m_exceptId+"]");
			}else{
				tvc.log("method---inExcept:exceptId---"+"[element.name："+element.name+",element.type："+element.type+"] not in ["+m_exceptId+"]");
			}
		}
	}
	return isExist;
 }


};
  /*是否开启调试模式*/
  tvc.debug = false;
  /*例外元素类型*/
  tvc.exceptType = [""];
  tvc.exceptName = [""];
  tvc.exceptId = [""];
  tvc.emptyValue = "";
  tvc.emptySelectValue = "-1";
  window.tvc = tvc;
}) (window);
